/**
 * Created by win10 on 2017/5/29.
 */
$(document).ready(function () {

//创建评论
    $('.comment').click(commentEdit);
//点赞or取消赞
    $('.dianzan').click(isLike);

//点赞or取消赞
    function isLike(){
        var likeCount = $(this).children('.dianzan-count').text();
        var liked=$(this).children('.liked').val();
        var fid=$(this).children(".fid").val();
        var userId=localStorage.getItem("userId");
      //  alert(userId);
        var obj=$(this)
        var data = {
            "customerId":userId,
            "id": fid,
        };
        if (liked==0){
            $.ajax({
                type: "get",
                url: '/api/v1/feedService/like/create',
                data:data,
                success: function (data) {
                    obj.children('img').attr('src','/static/h5/images/dianzan.png');
                //    alert("点赞成功！");
                    //点赞成功之后把值变成1
                    obj.children(".liked").val(1);
                    obj.children(".dianzan-count").text(" ");
                    likeCount=parseInt(likeCount)+parseInt(1);
                    obj.children('.feelicount').val(likeCount)
                    obj.children(".dianzan-count").text(likeCount);
                },
                error: function (msg) {
                    alert('请求失败');
                    console.log(msg)
                }
            });
        }
        else{
            $.ajax({
                type: "post",
                url: '/api/v1/feedService/like/delete',
                data:data,
                success: function (data) {
                    obj.children('img').attr('src','/static/h5/images/dianzan1.png');
                //    alert("取消成功！");
                    obj.children(".liked").val(0);
                    obj.children(".dianzan-count").text(" ");
                    likeCount=likeCount-1;
                    obj.children('.feelicount').val(likeCount)
                    obj.children(".dianzan-count").text(likeCount);
                },
                error: function (msg) {
                    alert('请求失败');
                    console.log(msg)
                }
            });
        }
    }


//新增评论
    function commentEdit() {

        var index = $(this).parents("li").index()//点击第几条数据的检索值
        var obj=$(this)
        var disCount = $(this).children('.pinglun-count').text();
        var liked=$(this).children('.liked').val();
        var userId=localStorage.getItem("userId");
        $(".cmt_cont").val("")//初始化

        $(".cmt_btm").animate({
            bottom: "0"
        }, 500)

        // 发表评论
        $("#cmt_issue").off("click").on("click",function () {
            var toArea = $("#share").children("ul").children("li").eq(index).find(".cmt_area")//添加数据位置
            console.log(toArea)
            var feedId = $(".fid").val()//评论
            var content = $(".cmt_cont").val()//内容
            var data ={
                "userId":userId,
                "content":content,
                "id":feedId
            }
            if(content.trim().length==0 || content==null){
                alert("请输入内容")
                return false;
            }else{
                $.ajax({
                    type:"post",
                    url:"/api/v1/feedService/comment/create",
                    data:data,
                    success:function(data){
               /*         issName=data.data.customer.name
                        obj.children(".pinglun-count").text(" ");
                        disCount=parseInt(disCount)+parseInt(1)
                        obj.children(".pinglun-count").text(disCount);
                        console.log(issName)
                        console.log(data.data.content)
                        toArea.prepend('<p class="issue_cont"><span class="issue_id">'+issName+'</span><span>：</span><span>'+data.data.content+'</span></p>')
                        $(".cmt_btm").animate({
                            bottom: "-40px"
                        },500)*/
                        window.location.reload();
                    },
                    error:function(err){
                        alert("issue failed")
                    }

                })
            }

            return false

        })
        return false
    }
//阻止事件冒泡
    $(".cmt_btm").on("click",function(){
        return false
    })

    $("body").on("click",function(){
        $(".cmt_btm").animate({
            bottom:"-40px"
        },500)
    })

    //删除评论
    $(".cmt_area").on("click",".issue_cont",function(){
        var userid=$(this).children(".userid").val();
        var userId=window.localStorage.getItem("userId");
         if(userid==userId) {
            var obj = $(this)
            $(".del_cmt").animate({
                bottom: "0"
            }, 300)
            var feedId = $(this).children(".commentId").val();
            var userId = localStorage.getItem("userId");
            var count = $(this).parent(".cmt_area").siblings(".share-method").children(".comment").children(".pinglun-count");
            var text = count.text();
            $(".del").on("click", function () {
                $(".del_cmt").animate({
                    bottom: "-75px"
                }, 300)
                // @parm feedid,userId,
                var data = {
                    "customerId": userId,
                    "commentId": feedId
                }
                $.ajax({
                    type: "post",
                    url: "/api/v1/feedService/comment/delete",
                    data: data,
                    success: function (data) {
                        obj.remove();
                        count.text(" ");
                        count.text(parseInt(text) - parseInt(1));
                    },
                    error: function (data) {

                    }
                });

            })
        }
        return false
    });
    $(".cancle").on("click", function () {
        return false
    })
    $("body").on("click", function () {
        $(".del_cmt").animate({
            bottom: "-75px"
        }, 300)
    })
});

//发送分享
$(".share_btn").click(createShare);

//创建分享
function createShare(){
    $("#family-circle").hide()
    $("#publish").show()

    $(".cancle_share").on("click",function(){
        $("#family-circle").show()
        $("#publish").hide()
    })

    $(".issue_share button").on("click",function(){
        if($(".share_cont").val()==""){
            $(".issue_share button").attr({
                "disabled":false
            })
        }else{
            $("#share_circle li").remove()
            $("#family-circle").show()
            $("#publish").hide()

            customerId=localStorage.getItem("userId");
            var content = $(".share_cont").val()
            var data = {
                "userId":customerId,
                "tagId":1,//分享
                "content":content,
            }
            $.ajax({
                type: "post",
                url: '/api/v1/feedService/create',//接口地址？
                data: data,
                success: function (data) {
                    console.log('请求成功');
                    /*$("#share_circle").prepend('<li class="container"><div class="row"><div class="col-xs-6 share-time">'
                        +'<div><span class="share-time-day">27</span><span class="share-time-mouth">3</span>月</div></div>'
                        +'<div class="col-xs-6 share-content"><img src="${ctx}/static/h5/images/share-img.png" class="share-img" alt=""/>'
                        +'<span class="share-text">'+issFont+'</span></div></div><div class="row share-method"><div class="col-xs-6 dianzan">'
                        +' <img src="${ctx}/static/h5/images/dianzan1.png" alt=""/><span class="dianzan-count">16</span></div><div class="col-xs-6 comment">'
                        +'<img src="${ctx}/static/h5/images/pinglun.png" alt=""/><span class="pinglun-count">6</span></div></div></li>')*/
                    window.location.href="/h5/index/home?userId="+customerId;
                },
                error: function (msg) {
                    console.log('请求失败');
                    console.log(msg)
//	                    $("#share ul").prepend('<li class="container"><div class="row"><div class="col-xs-6 share-time">'
//                  +'<div><span class="share-time-day">27</span><span class="share-time-mouth">3</span>月</div></div>'
//                  +'<div class="col-xs-6 share-content"><img src="../../images/share-img.png" class="share-img" alt=""/>'
//                  +'<span class="share-text">'+data.issFont+'</span></div></div><div class="row share-method"><div class="col-xs-6 dianzan">'
//                  +' <img src="../../images/dianzan1.png" alt=""/><span class="dianzan-count">16</span></div><div class="col-xs-6 comment">'
//                  +'<img src="../../images/pinglun.png" alt=""/><span class="pinglun-count">6</span></div></div></li>')
                }
            });

        }
    })
}

//分享时获取地理位置
$("#locMess").on("click",function(){
    var pos = new BMap.Geolocation()
    pos.getCurrentPosition(function(suc){
        $("#locMess").html("")
        $("#locMess").html(suc.address.street)
    })
})


//查看详情

/*
var userId=localStorage.getItem("userId");
$(".share-content").on('click',function () {
    var feedId=$(this).children(".fId").val();
    var tagId=1;
    details(feedId,tagId);
})
$(".comment").on("click",function () {
    var feedId=$(this).children(".fId").val();
    //var tagId=$(this).children(".tagid").val();
    var tagId=1;
    details(feedId,tagId);
})
//
function details(feedId,tagId){
    var userId=localStorage.getItem("userId");
    var url="/h5/feed/feedDetail?userId="+userId+"&id="+feedId+"&tagid="+tagId;
    window.location.href=url;
}*/
